package com.skt.manage.service.impl;

import java.util.List;

import com.skt.manage.mapper.DoctorsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.skt.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.skt.manage.domain.Doctors;
import com.skt.manage.mapper.DoctordepartmentsMapper;
import com.skt.manage.domain.Doctordepartments;
import com.skt.manage.service.IDoctordepartmentsService;

/**
 * 医生科室管理Service业务层处理
 * 
 * @author ffbaby
 * @date 2025-02-21
 */
@Service
public class DoctordepartmentsServiceImpl implements IDoctordepartmentsService 
{
    @Autowired
    private DoctordepartmentsMapper doctordepartmentsMapper;
    @Autowired
    private DoctorsMapper doctorsMapper;
    /**
     * 查询医生科室管理
     * 
     * @param assignmentId 医生科室管理主键
     * @return 医生科室管理
     */
    @Override
    public Doctordepartments selectDoctordepartmentsByAssignmentId(Long assignmentId)
    {
        return doctordepartmentsMapper.selectDoctordepartmentsByAssignmentId(assignmentId);
    }

    /**
     * 查询医生科室管理列表
     * 
     * @param doctordepartments 医生科室管理
     * @return 医生科室管理
     */
    @Override
    public List<Doctordepartments> selectDoctordepartmentsList(Doctordepartments doctordepartments)
    {
        return doctordepartmentsMapper.selectDoctordepartmentsList(doctordepartments);
    }

    /**
     * 新增医生科室管理
     * 
     * @param doctordepartments 医生科室管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDoctordepartments(Doctordepartments doctordepartments)
    {
        int rows = doctordepartmentsMapper.insertDoctordepartments(doctordepartments);
        insertDoctors(doctordepartments);
        return rows;
    }

//    /**
//     * 修改医生科室管理
//     *
//     * @param doctordepartments 医生科室管理
//     * @return 结果
//     */
//    @Transactional
//    @Override
//    public int updateDoctordepartments(Doctordepartments doctordepartments)
//    {
//        doctordepartmentsMapper.deleteDoctorsByDoctorId(doctordepartments.getAssignmentId());
//        insertDoctors(doctordepartments);
//        return doctordepartmentsMapper.updateDoctordepartments(doctordepartments);
//    }
/**
 * 修改医生科室管理
 *
 * @param doctordepartments 医生科室管理
 * @return 结果
 */

@Transactional
@Override
public int updateDoctordepartments(Doctordepartments doctordepartments) {
    // 删除旧的医生科室关联
    doctordepartmentsMapper.deleteDoctorsByDoctorId(doctordepartments.getAssignmentId());

    // 插入新的医生科室关联
    insertDoctors(doctordepartments);

    // 更新医生表中的科室字段
    Doctors doctor = new Doctors();
    doctor.setDoctorId(doctordepartments.getDoctorId());
    doctor.setSpecialization(doctordepartments.getDepartmentName());
    doctorsMapper.updateDoctors(doctor);

    // 更新医生科室管理的其他信息
    return doctordepartmentsMapper.updateDoctordepartments(doctordepartments);
}




    /**
     * 批量删除医生科室管理
     * 
     * @param assignmentIds 需要删除的医生科室管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDoctordepartmentsByAssignmentIds(Long[] assignmentIds)
    {
        doctordepartmentsMapper.deleteDoctorsByDoctorIds(assignmentIds);
        return doctordepartmentsMapper.deleteDoctordepartmentsByAssignmentIds(assignmentIds);
    }

    /**
     * 删除医生科室管理信息
     * 
     * @param assignmentId 医生科室管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDoctordepartmentsByAssignmentId(Long assignmentId)
    {
        doctordepartmentsMapper.deleteDoctorsByDoctorId(assignmentId);
        return doctordepartmentsMapper.deleteDoctordepartmentsByAssignmentId(assignmentId);
    }

    /**
     * 新增医生信息管理信息
     * 
     * @param doctordepartments 医生科室管理对象
     */
    public void insertDoctors(Doctordepartments doctordepartments)
    {
        List<Doctors> doctorsList = doctordepartments.getDoctorsList();
        Long assignmentId = doctordepartments.getAssignmentId();
        if (StringUtils.isNotNull(doctorsList))
        {
            List<Doctors> list = new ArrayList<Doctors>();
            for (Doctors doctors : doctorsList)
            {
                doctors.setDoctorId(assignmentId);
                list.add(doctors);
            }
            if (list.size() > 0)
            {
                doctordepartmentsMapper.batchDoctors(list);
            }
        }
    }
}
